﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace CallFlowDesigner
{
    public partial class TranscodeAudio : Form
    {
        public TranscodeAudio()
        {
            InitializeComponent();
            ofd.FileName = "";
        }

        public CallFlowDesigner cf;

        private void cmdFindSox_Click(object sender, EventArgs e)
        {
            ofd.Filter = "Sox Executable (sox.exe)|sox.exe";
            ofd.FileName = txtSox.Text;
            
            if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName.ToLower().EndsWith("sox.exe"))
            {
                Properties.Settings.Default.SoxPath = ofd.FileName;
                Properties.Settings.Default.Save();
                txtSox.Text = ofd.FileName;
            }
        }

        private void cmdFindLame_Click(object sender, EventArgs e)
        {
            ofd.Filter = "Lame Executable (lame.exe)|lame.exe";
            ofd.FileName = txtSox.Text;

            if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName.ToLower().EndsWith("lame.exe"))
            {
                Properties.Settings.Default.LamePath = ofd.FileName;
                Properties.Settings.Default.Save();
                txtLame.Text = ofd.FileName;
            }
        }

        private void linkGoToSox_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo("http://sox.sourceforge.net/");
            psi.UseShellExecute = true;
            Process.Start(psi);
        }

        private void lnkGoToLame_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo psi = new ProcessStartInfo("http://www.google.com/search?hl=en&q=lame.exe&btnG=Search");
            psi.UseShellExecute = true;
            Process.Start(psi);
        }

        private void TranscodeAudio_Load(object sender, EventArgs e)
        {
            if (!Properties.Settings.Default.RunTranscode)
            {
                MessageBox.Show("This small utility has been created to help you convert mp3 files\r\ninto an audio format that can be played on the phone network\r\n\r\nIt uses two open source utilities that are easily found and downloaded, sox.exe and lame.exe\r\nYou will need to download these utilities and make sure the app knows where they are", "CallFlow Designer", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Properties.Settings.Default.RunTranscode = true;
                Properties.Settings.Default.Save();
            }

            txtSox.Text = Properties.Settings.Default.SoxPath;
            txtLame.Text = Properties.Settings.Default.LamePath;

            if (txtSox.Text.Length >0 && ! File.Exists(txtSox.Text))
            {
                txtSox.Text = "";
                Properties.Settings.Default.SoxPath ="";
                Properties.Settings.Default.Save();
            }

            if (txtLame.Text.Length > 0 && !File.Exists(txtLame.Text))
            {
                txtLame.Text = "";
                Properties.Settings.Default.LamePath = "";
                Properties.Settings.Default.Save();
            }
        
        }

        private void cmdSelectMp3_Click(object sender, EventArgs e)
        {
            ofd.Filter = "MP3 file (*.mp3)|*.mp3";
            if (ofd.ShowDialog() == DialogResult.OK && ofd.FileName.ToLower().EndsWith(".mp3"))
            {
                txtMp3.Text = ofd.FileName;
            }
        }

        private void cmdSelectWav_Click(object sender, EventArgs e)
        {
            if (txtMp3.Text.EndsWith(".mp3"))
            {
                sfd.FileName = txtMp3.Text.Replace(".mp3", ".wav");
            }
            sfd.Filter = "wav file (*.wav)|*.wav";
            if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.ToLower().EndsWith(".wav"))
            {
                txtWav.Text = sfd.FileName;
            }
        }

        private void cmdTranscode_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            
            string decodeToWav = "\"" + txtLame.Text + "\" --decode \"" + txtMp3.Text + "\" \"" +System.IO.Path.GetTempPath()+"tempfile.wav\"";
          
            string encodeToULaw = "\"" + txtSox.Text + "\" \""+ System.IO.Path.GetTempPath()+"tempfile.wav\" -t wav -U -c 1 -b -r 8000 \"" + txtWav.Text +"\"";// c.wav -t wav -U -c 1 -b -r 8000 out.wav
          
            
            ProcessStartInfo psi = new ProcessStartInfo(decodeToWav);
            psi.UseShellExecute = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            Process ps = Process.Start(psi);

            while (!ps.HasExited) Thread.Sleep(10);
    
            psi = new ProcessStartInfo(encodeToULaw);
            psi.UseShellExecute = false;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            ps = Process.Start(psi);
            
            while (!ps.HasExited) Thread.Sleep(10);

            File.Delete(System.IO.Path.GetTempPath()+"tempfile.wav");

            this.Cursor = Cursors.Default;
            if (MessageBox.Show("Transcoding completed\r\nWould you like to upload this straight away?", "CallFlow Designer", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                cf.ofd.FileName = txtWav.Text;
                cf.StartBackgroundThread(new DoWorkEventHandler(cf.backgroundThread_UploadFile));
            }
        }


    }
}
